﻿namespace SealedCrateOpener
{
    using Styx;
    using Styx.Common;
    using Styx.Common.Helpers;
    using Styx.CommonBot.Frames;
    using Styx.CommonBot.Inventory;
    using Styx.CommonBot.Profiles;
    using Styx.Helpers;
    using Styx.Pathing;
    using Styx.Plugins;
    using Styx.WoWInternals;
    using Styx.WoWInternals.WoWObjects;
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Drawing;
    using System.IO;
    using System.Linq;
    using System.Runtime.InteropServices;
    using System.Text;
    using System.Threading;
    using System.Windows.Forms;
    using System.Windows.Media;
    using System.Xml.Linq;

    public class SealedCrateOpener : HBPlugin
    {
        public override string Name { get { return "SealedCrateOpener"; } }
        public override string Author { get { return "Lbniese"; } }
        public override Version Version { get { return new Version(0,0,0,9); } }

        private HashSet<uint> _itemUseOnOne = new HashSet<uint>() {88496,};

        private HashSet<uint> _itemUseOnFive = new HashSet<uint>() {
        };

        private HashSet<uint> _itemUseOnTen = new HashSet<uint>() {
        };

        private HashSet<uint> _itemRequiresSleep = new HashSet<uint>() {
        };

        private static Stopwatch sw = new Stopwatch();

        public override void Pulse()
        {
            if (StyxWoW.Me.IsActuallyInCombat
                || StyxWoW.Me.Mounted
                || StyxWoW.Me.IsDead
                || StyxWoW.Me.IsGhost
                //|| Styx.CommonBot.LootTargeting.LootMobs
                ) {
                return;
            }

            if (!sw.IsRunning) {
                sw.Start();
            }

            if (sw.Elapsed.TotalSeconds > 1) { // throttle to 1s
                foreach (WoWItem item in ObjectManager.GetObjectsOfType<WoWItem>()) { // iterate over every item
                    if (item != null && item.BagSlot != -1) { // check if item exists and is in bag
                        if (_itemUseOnFive.Contains(item.Entry)) { // stacks of 5
                            if (item.StackCount >= 5) {
                                this.useItem(item);
                            }
                        } else if (_itemUseOnTen.Contains(item.Entry)) { // stacks of 10
                            if (item.StackCount >= 10) {
                                this.useItem(item);
                            }
                        } else if (_itemUseOnOne.Contains(item.Entry)) { // stacks of 1
                            if (item.StackCount >= 1) {
                                this.useItem(item);
                            }
                        }
                    }
                }

                StyxWoW.SleepForLagDuration();
                sw.Reset();
                sw.Start();
            }
        }

        private void useItem(WoWItem item)
        {
            Logging.Write(LogLevel.Normal, Colors.DarkRed, "[{0} {1}]: Using {2} we have {3}", this.Name, this.Version, item.Name, item.StackCount);
			
            if (_itemRequiresSleep.Contains(item.Entry)) {
                // some (soulbound) items require an additional sleep to prevent a loot bug
                StyxWoW.SleepForLagDuration();
            }
            Lua.DoString("UseItemByName(\"" + item.Name + "\")");
            StyxWoW.SleepForLagDuration();
        }
    }
}